import type { DataNode } from "ant-design-vue/es/vc-tree/interface";
import type { NodejsMirror, NodeVersionInfo } from "./types";
import npmmirror from "./npmmirror";

export const nodejsMirrorList: NodejsMirror[] = [npmmirror];

export const npmRegistryList = [
  { value: "https://registry.npmmirror.com/" },
  { value: "https://mirrors.huaweicloud.com/repository/npm/" },
  { value: "https://mirrors.cloud.tencent.com/npm/" },
  // ...
];
export const electronMirrorList = [
  { value: "https://cdn.npmmirror.com/binaries/electron/" },
  { value: "https://mirrors.huaweicloud.com/electron/" },
  // ...
];
export const electronBuilderBinariesMirrorList = [
  { value: "https://npmmirror.com/mirrors/electron-builder-binaries/" },
  { value: "https://mirrors.huaweicloud.com/electron-builder-binaries/" },
  // ...
];
export const pythonMirrorList = [
  { value: "https://npmmirror.com/mirrors/python/" },
  { value: "https://mirrors.huaweicloud.com/python/" },
  // ...
];

export function versionList2TreeData(versionList: NodeVersionInfo[]): DataNode[] {
  const versions: Record<string, DataNode> = Object.create(null);
  for (const it of versionList) {
    const version = it.version;
    const [major] = version.split(".");
    if (!versions[major]) {
      versions[major] = { title: `v${major}.x.x`, key: major, top: true, children: [] };
    }
    versions[major].children!.push({
      title: version,
      key: version,
      lastModified: it.lastModified,
    });
  }
  const list = Object.values(versions);
  list.reverse();
  return list;
}
